//
//  CYStateTwoViewController.m
//  cyydt
//
//  Created by chu on 2019/1/21.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYStateTwoViewController.h"
#import "CYMinePickerView.h"
#import "CYCategrayModel.h"
#import "CYSelCategaryView.h"
@interface CYStateTwoViewController ()
{
    NSString *_gender;
    NSNumber *_birthday;
    NSNumber *_budget;
    NSString *_industry;
    NSString *_industryId;
}
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet LPButton *nanBtn;
@property (weak, nonatomic) IBOutlet LPButton *nvbtn;
@property (weak, nonatomic) IBOutlet UIButton *nianlingBtn;
@property (weak, nonatomic) IBOutlet UIButton *jineBtn;
@property (weak, nonatomic) IBOutlet UIButton *xiangmuBtn;
@property (weak, nonatomic) IBOutlet UIButton *doneBtn;

@property (weak, nonatomic) IBOutlet UILabel *ageLabel;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *likeLabel;

@property (nonatomic, strong) NSMutableArray *ageArr;
@property (nonatomic, strong) NSMutableArray *moneyArr;
@property (nonatomic, strong) NSMutableArray *categaryArr;

@end

@implementation CYStateTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"状态";
    self.doneBtn.layer.cornerRadius = self.doneBtn.frame.size.height / 2;
    self.doneBtn.layer.masksToBounds = YES;
    
    self.nanBtn.layer.cornerRadius = 5;
    self.nanBtn.layer.masksToBounds = YES;
    
    self.nvbtn.layer.cornerRadius = 5;
    self.nvbtn.layer.masksToBounds = YES;
    
    self.backView.layer.cornerRadius = 10;
    [self dropShadowWithOffset:CGSizeMake(0, 0)
                        radius:5
                         color:[UIColor blackColor]
                       opacity:0.14];

    self.nanBtn.style = LPButtonStyleTop;
    self.nvbtn.style = LPButtonStyleTop;
    self.nanBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.nvbtn.titleLabel.textAlignment = NSTextAlignmentCenter;

    [self getStagesWithType:@"2"];
    [self getStagesWithType:@"3"];
    [self getAllCategary];
    
    [self rightBarItemWithTitle:@"跳过"];
}

- (void)RighttouchEvent{
    [[AppDelegate sharedAppDelegate] enterRootVc];
}

- (void)dropShadowWithOffset:(CGSize)offset
                      radius:(CGFloat)radius
                       color:(UIColor *)color
                     opacity:(CGFloat)opacity {
//    CGMutablePathRef path = CGPathCreateMutable();
//    CGPathAddRect(path, NULL, self.backView.bounds);
//    self.backView.layer.shadowPath = path;
//    CGPathCloseSubpath(path);
//    CGPathRelease(path);
    
    self.backView.layer.shadowColor = color.CGColor;
    self.backView.layer.shadowOffset = offset;
    self.backView.layer.shadowRadius = radius;
    self.backView.layer.shadowOpacity = opacity;
    self.backView.clipsToBounds = NO;
}

- (IBAction)nanAction:(LPButton *)sender {
    if (sender == self.nanBtn) {
        [self.nanBtn setTitleColor:RGBOF(0x333333) forState:UIControlStateNormal];
        [self.nanBtn setBackgroundColor:RGBOF(0xededed)];
        [self.nvbtn setTitleColor:RGBOF(0x999999) forState:UIControlStateNormal];
        [self.nvbtn setBackgroundColor:[UIColor whiteColor]];
        self.nanBtn.selected = YES;
        self.nvbtn.selected = NO;
        _gender = @"1";
    }else{
        [self.nanBtn setTitleColor:RGBOF(0x999999) forState:UIControlStateNormal];
        [self.nanBtn setBackgroundColor:[UIColor whiteColor]];
        [self.nvbtn setTitleColor:RGBOF(0x333333) forState:UIControlStateNormal];
        [self.nvbtn setBackgroundColor:RGBOF(0xededed)];
        self.nanBtn.selected = NO;
        self.nvbtn.selected = YES;
        _gender = @"2";
    }
}

- (IBAction)doneAction:(UIButton *)sender {
    if ([NSString stringIsNull:_gender]) {
        [self.view makeToast:@"请选择性别" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (!_birthday) {
        [self.view makeToast:@"请选择年龄" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (!_budget) {
        [self.view makeToast:@"请选择投资金额" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:_industryId]) {
        [self.view makeToast:@"请选择创业偏好" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:self.type]) {
        [self.view makeToast:@"请选择创业状态" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    [EasyShowLodingView showLoding];
    NSDictionary *param = @{@"gender":_gender, @"birthday":_birthday, @"budget":_budget, @"industry":_industryId, @"type":self.type};
    
    NSString *urlString = [User_HOST stringByAppendingString:@"user/initPage"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    [manager.requestSerializer setValue:[self getUUID] forHTTPHeaderField:@"uuid"];

    //接收类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"image/jpeg",
                                                         @"image/png",
                                                         @"application/octet-stream",
                                                         @"text/json",
                                                         nil];
    [manager POST:urlString parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //打印下上传进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [EasyShowLodingView hidenLoding];
        
        NSLog(@"responseObject--- %@",responseObject);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *responseResult = (NSDictionary *)responseObject;
            if ([responseResult[Response_code] integerValue] == Response_value) {
//                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"message"] duration:1.5 position:CSToastPositionCenter];
                [[AppDelegate sharedAppDelegate] enterRootVc];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"message"] duration:1.5 position:CSToastPositionCenter];
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error -- %@",error);
        [EasyShowLodingView hidenLoding];
        
    }];
}



- (IBAction)tapAction:(UIButton *)sender {
    if (sender == self.nianlingBtn) {
        [self selNianLing];
    }else if (sender == self.jineBtn){
        [self selTouZiYuSuan];
    }else{
        [self selHangYe];
    }
}

#pragma mark - 选择年龄范围
- (void)selNianLing{
    NSMutableArray *muarr = [NSMutableArray array];
    for (NSDictionary *dic in self.ageArr) {
        [muarr addObject:dic[@"name"]];
    }
    CYMinePickerView *headView = [[CYMinePickerView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 305) DataArr:muarr components:1];
    headView.titleLable.text = @"请选择年龄段";
    headView.otherBlock = ^(NSString * _Nonnull value, NSInteger index) {
        [self.nianlingBtn setTitle:[NSString stringWithFormat:@" %@",value] forState:UIControlStateNormal];
        NSDictionary *dic = self.ageArr[index];
        self->_birthday = dic[@"id"];
    };
    [GKCover translucentCoverFrom:[UIApplication sharedApplication].keyWindow content:headView animated:YES showBlock:^{
        // 显示出来时的block
        NSLog(@"弹窗显示了，6不6");
    } hideBlock:^{
        // 移除后的block
        NSLog(@"弹窗消失了，555");
    }];
}

#pragma mark - 请选择投资预算
- (void)selTouZiYuSuan{
    NSMutableArray *muarr = [NSMutableArray array];
    for (NSDictionary *dic in self.moneyArr) {
        [muarr addObject:dic[@"name"]];
    }
    CYMinePickerView *headView = [[CYMinePickerView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 305) DataArr:muarr components:1];
    headView.titleLable.text = @"请选择投资预算";
    headView.otherBlock = ^(NSString * _Nonnull value, NSInteger index) {
        [self.jineBtn setTitle:[NSString stringWithFormat:@" %@",value] forState:UIControlStateNormal];
        NSDictionary *dic = self.moneyArr[index];
        self->_budget = dic[@"id"];
    };
    [GKCover translucentCoverFrom:[UIApplication sharedApplication].keyWindow content:headView animated:YES showBlock:^{
        // 显示出来时的block
        NSLog(@"弹窗显示了，6不6");
    } hideBlock:^{
        // 移除后的block
        NSLog(@"弹窗消失了，555");
    }];
}

#pragma mark - 选择感兴趣的行业
- (void)selHangYe{
    NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"CYSelCategaryView" owner:self options:nil];
    CYSelCategaryView *backView = [nibView objectAtIndex:0];
    backView.frame = CGRectMake(0, 0, kWindowW, 410);
    backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    backView.dataSourceArr = self.categaryArr;
    backView.singlOrMore = YES;
    __weak typeof(self)weakself = self;
    backView.duoxuanBlock = ^(NSArray * _Nonnull valueArr) {
        NSMutableArray *muArr = [NSMutableArray array];
        NSMutableArray *namearr = [NSMutableArray array];
        for (CYCategrayModel *model in valueArr) {
            [muArr addObject:[model.catid stringValue]];
            [namearr addObject:model.catname];
        }
        self->_industry = [namearr componentsJoinedByString:@","];
        self->_industryId = [muArr componentsJoinedByString:@","];
        [weakself.xiangmuBtn setTitle:self->_industry forState:UIControlStateNormal];
    };
    [GKCover translucentCoverFrom:[UIApplication sharedApplication].keyWindow content:backView animated:YES showBlock:^{
        // 显示出来时的block
        NSLog(@"弹窗显示了，6不6");
    } hideBlock:^{
        // 移除后的block
        NSLog(@"弹窗消失了，555");
    }];
}

#pragma mark - 获取创业属性
- (void)getStagesWithType:(NSString *)type{
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"stages"];
    NSDictionary *param = @{@"type":type};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"获取创业属性 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if ([responseResult[@"data"] isKindOfClass:[NSArray class]]) {
                    NSArray *data = responseResult[@"data"];
                    if ([type isEqualToString:@"2"]) {
                        [self.moneyArr addObjectsFromArray:data];
                    }else if ([type isEqualToString:@"3"]){
                        [self.ageArr addObjectsFromArray:data];
                    }
                }
            }else{
                
            }
        }
    }];
}

#pragma mark - 获取所有分类
- (void)getAllCategary{
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"categories"];
    NSDictionary *param = @{@"type":@3};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"获取所有分类 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if ([responseResult[@"data"] isKindOfClass:[NSArray class]]) {
                    NSArray *data = responseResult[@"data"];
                    [self.categaryArr removeAllObjects];
                    for (NSDictionary *dic in data) {
                        NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithDictionary:dic];
                        [muDic setObject:dic[@"name"] forKey:@"catname"];
                        [muDic setObject:dic[@"id"] forKey:@"catid"];
                        [muDic setObject:dic[@"childCates"] forKey:@"child"];
                        [muDic removeObjectForKey:@"id"];
                        [muDic removeObjectForKey:@"name"];
                        [muDic removeObjectForKey:@"childCates"];
                        CYCategrayModel *model = [CYCategrayModel modelWithDictionary:muDic];
                        [self.categaryArr addObject:model];
                    }
                }
            }else{
                
            }
        }
    }];
}

/**  卸载应用重新安装后会不一致*/
- (NSString *)getUUID{
    return [UIDevice currentDevice].identifierForVendor.UUIDString;;
}


- (NSMutableArray *)ageArr{
    if (!_ageArr) {
        _ageArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _ageArr;
}

- (NSMutableArray *)moneyArr{
    if (!_moneyArr) {
        _moneyArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _moneyArr;
}

- (NSMutableArray *)categaryArr{
    if (!_categaryArr) {
        _categaryArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _categaryArr;
}

@end
